<?php

include_once("../service/categoryService.php");
include_once("../util/characterUtils.php");

$name = $_POST["catcontent"];
$order = $_POST["catorder"];
$result = countCatByOrder($order);
$row = ConnectionManager::getInstance()->getNumRows();
if ($row > 0 || $order < 1) {
    Header("Location: ../admin-page/addCategory.php?message_code=error");
    return;
} else {
    $result = getAllCategory();
    $flag = true;
    while ($cat = ConnectionManager::getInstance()->fetchArray()) {
        if (removeSignOfString($cat["CAT_NAME"]) == removeSignOfString($name)) {
            $flag = false;
            break;
        }
    }
    if ($flag) {
        addCategory($name, $order);
        Header("Location: ../admin-page/addCategory.php?message_code=ok");
        return;
    } else {
        Header("Location: ../admin-page/addCategory.php?message_code=unique");
        return;
    }
}
?>
